package zmee.pur.tnd.formplugin;

import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;

import java.util.*;

import static kd.bos.orm.query.QCP.equals;

/**
 * @Author: xw
 * @Date: 2022/11/09/14:17
 * @Description: 寻源协同：寻源项目f7过滤
 */
public class ProjectF7Plugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    /**
     * 寻源项目标识
     */
    private static final String KEY_PROJECT = "project";

    /**
     * 投标截止时间
     */
    private static final String KEY_STOPBIDDATE = "stopbiddate";

    /**
     * 答疑截止时间
     */
    private static final String KEY_ANSWERDATE = "answerdate";

    /**
     * 开标情况
     */
    private static final String KEY_OPENSTATUS = "openstatus";

    /**
     * 单据状态
     */
    private static final String KEY_BILLSTATUS = "billstatus";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        BasedataEdit fieldEdit = this.getView().getControl(KEY_PROJECT);
        fieldEdit.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        //寻源项目过滤
        if (StringUtils.equals(name, KEY_PROJECT)) {
            ListShowParameter showParameter = (ListShowParameter) event.getFormShowParameter();
            //项目启动单据状态为已审核
            QFilter filter1 = new QFilter(KEY_BILLSTATUS, equals, "C");
            //开标情况为待开标
            QFilter filter2 = new QFilter(KEY_OPENSTATUS, equals, "1");
            //报价截止时间-答疑截止时间大于等于15（天）
            QFilter filter3 = new QFilter(KEY_STOPBIDDATE + "-" + KEY_ANSWERDATE, ">=", 15);
            QFilter filter4 = new QFilter(KEY_STOPBIDDATE + "-" + KEY_ANSWERDATE, "=", 0);
            QFilter filter5 = new QFilter(KEY_ANSWERDATE, ">=", new Date());
            QFilter filter = filter1.and(filter2.and(filter3).or(filter4).and(filter5));
            List<QFilter> filters = new ArrayList<>();
            filters.add(filter);
            showParameter.getListFilterParameter().setQFilters(filters);
        }
    }
}